<%-- 
    Document   : TestConnectionByJDBC
    Created on : 2009/12/18, 上午 10:12:55
    Author     : gmlin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*,java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>TestQueryByJDBC</title>
    </head>
    <body>

        <%
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;

            try{
                DriverManager.registerDriver((Driver) (Class.forName(
                        "com.mysql.jdbc.Driver").newInstance()));
                conn = DriverManager.getConnection("jdbc:mysql://gm-server:3306/webdb?useUnicode=true&characterEncoding=utf-8", "webap","t0255");

                stmt = conn.createStatement();
                rs = stmt.executeQuery("select count(*) from school_ext where emp_name like '%林%'");

                out.println("測試成功，以下為測試資料<br/>");

                while (rs.next()) {
                    for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) {
                        out.print(rs.getObject(j + 1));
                        out.print("\t");
                    }
                    out.println();
                }
            }
            catch(Exception se){
                se.printStackTrace();
            }
            finally{
                if (rs != null) {
                    try {
                        rs.close();
                        rs = null;
                    } catch (Exception se) {
                        se.printStackTrace();
                    }
                } //釋放沒用的ResultSet
                if(stmt!=null){
                    stmt.close();
                    stmt=null;
                }
                if (conn != null) {
                    conn.close();
                    conn=null;
                }
            }
        %>
    </body>
</html>
